﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Filter;
using COFE.Data.Utils.ThirdParty;
using System.Linq.Expressions;

namespace COFE.Data.Filter
{
    public class DBExpireEntryFilterExpression
        : EntryFilterExpression<COFE.Data.Filter.DBExpireEntryFilter>
    {
        public DBExpireEntryFilterExpression()
            : base()
        {

        }

        public override Expression<Func<Entry, bool>> getExpression(
            IEntryFilter filter, IOptionFilter[] options)
        {
            var entryFilter = filter as COFE.Data.Filter.DBExpireEntryFilter;
            Expression<Func<Entry, bool>> exp = e => true;
            if (entryFilter.ExpiredType == DBExpiredType.Property)
            {
                var expireTime =
                    DateTime.UtcNow.Subtract(COFEDBShared.TTLPropertyValues);
                exp = (e) => expireTime > e.PropertyRefreshTime;
            }
            else
                if (entryFilter.ExpiredType == DBExpiredType.List)
                {
                    var expireTime =
                        DateTime.UtcNow.Subtract(COFEDBShared.TTLEntryList);
                    exp = (e) => (e is FolderEntry) && expireTime > (e as FolderEntry).LastListTime;
                }

            if (filter.Negative)
                return exp.Not();
            return exp;
        }
    }
}
